package com.javalong.affairs.module.main.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;

import com.javalong.affairs.R;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.DataType;
import com.javalong.affairs.base.constant.MapKey;
import com.javalong.affairs.base.data.AllData;
import com.javalong.affairs.base.fragment.BaseFragment;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.SimpleMessage;
import com.javalong.affairs.base.ui.SimplePullRefreshList;
import com.javalong.affairs.module.main.activity.MainActivity;
import com.javalong.affairs.module.main.activity.NewMainActivity;
import com.javalong.affairs.module.main.adapter.NewMsgAdapter;
import com.javalong.affairs.module.notifything.activity.MsgDetailActivity;
import com.javalong.affairs.module.notifything.activity.NotificationActivity;
import com.javalong.affairs.module.notifything.adapter.NotificationAdapter;
import com.javalong.affairs.module.renwujinzhang.activity.SuperviseActivity;
import com.javalong.affairs.module.renwujinzhang.activity.WatchRunTaskActivity;
import com.javalong.affairs.module.renwujinzhang.activity.audit.AuditListActivity;
import com.javalong.affairs.module.renwujinzhang.activity.complete.CompleteListActivity;
import com.javalong.affairs.module.renwujinzhang.activity.receive.ReceiveListActivity;
import com.javalong.affairs.module.renwujinzhang.activity.supervise.SuperviseListActivity;
import com.javalong.affairs.module.renwujinzhang.model.NoticeMsg;
import com.javalong.affairs.module.renwujinzhang.model.Task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by javalong on 2016/5/15.
 */
public class NewMsgFragment extends BaseFragment implements
        AdapterView.OnItemClickListener, SimplePullRefreshList.OnLoadSuccess {

    private SimplePullRefreshList lv_receiver;
    private NewMsgAdapter ad_notification;
    private List<NoticeMsg> msgList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_newmsg,
                container, false);
        lv_receiver = (SimplePullRefreshList) view
                .findViewById(R.id.lv_receiver);
        msgList = AllData.GetInstance().appendListData(
                R.string.data_notification_receive,
                new ArrayList<NoticeMsg>());
        ad_notification = new NewMsgAdapter(getActivity(), msgList);
        lv_receiver.setAdapter(ad_notification);
        lv_receiver.initData(getMapData());
        lv_receiver.refresh();
        lv_receiver.setOnItemClickListener(this);
        lv_receiver.setOnLoadSuccess(this);
        return view;
    }

    private Map<String, Object> getMapData() {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put(MapKey.ACTION_NAME, "Task");
        param.put(MapKey.METHOD_NAME, "outList_countNotice");
        param.put(MapKey.DATA_RESID, R.string.data_notification_receive);
        param.put(MapKey.DATA_TYPE, DataType.LIST_NOTICEMSG);
        return param;
    }

    @Override
    public void refresh() {
        super.refresh();
        lv_receiver.refresh();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
                            long id) {
        NoticeMsg msgInfo = msgList.get(position - 1);
        if ("紧急待办任务".equals(msgInfo.getType())) {
            ReceiveListActivity.toThisActivity(getActivity());
        } else if ("待审核任务".equals(msgInfo.getType())) {
            AuditListActivity.toThisActivity(getActivity());
        } else if ("审核不通过任务".equals(msgInfo.getType())) {
            AuditListActivity.toThisActivity(getActivity());
        } else if ("新通知消息".equals(msgInfo.getType())) {
            ReceiveListActivity.toThisActivity(getActivity());
        } else if ("待监督任务".equals(msgInfo.getType())) {
            SuperviseListActivity.toThisActivity(getActivity());
        } else if ("待评审任务".equals(msgInfo.getType())) {
            CompleteListActivity.toThisActivity(getActivity());
        }
    }

    @Override
    public void onSuccess() {
        if (msgList == null) return;
        int total = 0;
        for (NoticeMsg noticeMsg : msgList) {
            total += noticeMsg.getNum();
        }
        if(total>0){
            Activity a = getActivity();
            if(a instanceof NewMainActivity){
                NewMainActivity mainActivity = (NewMainActivity) a;
                mainActivity.updateMsgCount(total);
            }
        }
    }
}
